// navptnpc.txt
// A hostile creature which fights a running battle, running from nav pt to
// nav pt ahead of the pc.
// Runs whenever its health gets a little low (below 3/4)
// Memory Cells:
//   Cell 0 - First nav point it runs to.
//   Cell 1 - Last nav point in its chain
//   Cell 2,3 - SDF to increment when it is killed.
//   Cell 4 - Number of state when talked to. Plays default text if 0.

begincreaturescript;

variables;

short i,target;
short current_flee_goal;

body;

beginstate INIT_STATE;
	current_flee_goal = get_memory_cell(0) - 1;
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(2) != 0) || (get_memory_cell(3) != 0))
		inc_flag(get_memory_cell(2),get_memory_cell(3),1);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	

	if ((get_health(ME) < (1 * get_max_health(ME)) / 2) &&
	  (current_flee_goal < get_memory_cell(1))) {
		current_flee_goal = current_flee_goal + 1;
		approach_nav_point(ME,current_flee_goal,3);
		set_state(4);
		}

	//fidget(ME,20);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_health(ME) < (3 * get_max_health(ME)) / 5) &&
	  (current_flee_goal < get_memory_cell(1))) {
		current_flee_goal = current_flee_goal + 1;
		approach_nav_point(ME,current_flee_goal,3);
		set_state(4);
		}

	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate 4; // fleeing state
	set_act_at_dist(ME,1);
	if (approach_nav_point(ME,current_flee_goal,3))
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
	if (get_memory_cell(4) == 0) {
		print_str("Talking: It doesn't respond.");
		}
		else begin_talk_mode(get_memory_cell(4));
break;